package com.eascs.partner.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.partner.dao.JxsPartnerDao;
import com.eascs.partner.form.PartnerQueryForm;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/jxsPartner")
public class JxsPartnerController extends BaseMasterViewController {

	@Reference
	SysOrgService sysOrgService;

	@Reference
	SysPermissionService sysPermissionService;

	@Autowired
	private JxsPartnerDao jxsPartnerDao;

	@RequestMapping("/choosePartner")
	public String choosePartner(String brandId) {
		ViewContext.put("brandId", brandId);
		return "/partner/jxsPartner_choose";
	}

	/**
	 * @param params
	 * 
	 */
	@RequestMapping("/index")
	public String findPartnerInfo(PartnerQueryForm params) {
		params.setPageSize(10); // 每页数据量
		String o2ourl = "/jxsPartner/index";
		String partnerName = params.getPartnerName() == null ? "" : params.getPartnerName();

		String brandName = params.getBrandName() == null ? "" : params.getBrandName();
		String hostProvince = params.getHostProvince() == null ? "" : params.getHostProvince();
		String createTime = params.getCreateTime() == null ? "" : params.getCreateTime();
		String createEndTime = params.getCreateEndTime() == null ? "" : params.getCreateEndTime();
		String operator = params.getOperator() == null ? "" : params.getOperator();

		String Status = params.getStatus() == null ? "" : params.getStatus();
		o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
		o2ourl = UrlUtil.appendUrl(o2ourl, "createTime", createTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "createEndTime", createEndTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "Status", Status);
		o2ourl = UrlUtil.appendUrl(o2ourl, "operator", operator);
		o2ourl = o2ourl + "&pageIndex=";
		/**
		 * @param 查询相关信息，放入页面相关数据
		 */
		PageResult<Object[]> pageResult = jxsPartnerDao.findPartnerInfoByPage(params, params.getPageIndex(),
				params.getPageSize());
		String[] titles = { "申报单号", "经办人", "主办省份", "一批商渠道名称", "一批商分类", "所属品牌", "融资额度(万元)", "创建时间", "状态" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("O2OPROVINCE"), null, null, null,
				null, null, new SelectDataOption("parWFStutus") };
		Button[] Button = new Button[] { ButtonHelper.createAjaxButton(null, "/jxsPartner/rest/view_edit?partnerId="),
				null };
		Button delete = ButtonHelper.createAjaxConfirmButton("删除", "/jxsPartner/rest/delete?partnerId=", "editPartner");
		delete.setPermissionCode("deletePartner");
		delete.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if ("-1".equals(params[9] + "") || "3".equals(params[9] + ""))
					return true;
				else
					return false;
			}
		});
		Button startFlow = ButtonHelper.createAjaxButton("发起流程", "/jxsPartner/rest/jxsStartFlow?id=",
				"startPartnerFlow");// parameter为合作方id
		startFlow.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if ("-1".equals(params[9] + "") || "3".equals(params[9] + ""))
					return true;
				else
					return false;
			}
		});
		User user = UserHolder.getUser();
		Button edit = ButtonHelper.createAjaxButton("编辑", "/jxsPartner/rest/view_edit?partnerId=", "editPartner");// parameter为合作方id
		edit.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (StringUtils.isBlank((String)params[10])) {
					if ("-1".equals(params[9] + "") || "3".equals(params[9] + "")) {
						if (user.getNo().equals(params[10] + "") || user.isSuperAdministrator()) {
							return true;
						}
					} else if ("0".equals(params[9] + "")) {
						ModelResult<Boolean> result = sysPermissionService.checkAuth(user.getNo(), "ZBPPSH");
						if (result.isSuccess() && result.getModel()) {
							return true;
						}
					}
				}
				return false;
			}
		});
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, o2ourl);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildSelectBox(false);
		paginationInfo.setTitleButtons(Button);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("partner", params);
		ViewContext.put("Status", params.getStatus());
		return "partner/jxs_index";

	}

	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "jxsPartnerManage");
	}

}
